package com.heny.demo.strategy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class StrategyFactory {
    private final Map<String, FileImportStrategy> strategies;

    @Autowired
    public StrategyFactory(List<FileImportStrategy> strategies) {
        this.strategies = new HashMap<>();
        for (FileImportStrategy strategy : strategies) {
            this.strategies.put(strategy.getClass().getSimpleName(), strategy);
        }
    }

    public FileImportStrategy getStrategy(String fileName) {
        String strategyName = getFileType(fileName).toLowerCase() + "FileImportStrategy";
        return strategies.get(strategyName);
    }
    private String getFileType(String fileName) {
        return fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();
    }
}